
	var timerloading;
	var uploadProgress = 0;
	var uploadTimer;
	var uploadFileName;
	var serverResultApkInfo;

	function getfilename(value) {

        if(value == null || value.indexOf(".apk") < 0 ) {
        	alert("上传文件无效");
        	return;
        }

        var selected = document.getElementById("selected");
        var uploadarea = document.getElementById("uploadarea");
        var loadingarea = document.getElementById("loadingarea");
        var updatesuccess = document.getElementById("updatesuccess");
        selected.style.display = "none";
        uploadarea.style.display = "none";
        updatesuccess.style.display = "none";

        loadingarea.style.display = 'block';

		pushApp(value);
        loading();
        updateProgress();
    }

    function startUploadFile() {
            var selected = document.getElementById("selected");
            var uploadarea = document.getElementById("uploadarea");
            var loadingarea = document.getElementById("loadingarea");
            var updatesuccess = document.getElementById("updatesuccess");
            var loadmsg = document.getElementById("loadmsg");

			loadmsg.style.visibility = "visible";
            selected.style.display = "none";
            uploadarea.style.display = "none";
            updatesuccess.style.display = "none";
            loadingarea.style.display = 'block';
            serverResultApkInfo = null;
            uploadProgress = 0;

            loading();
            updateProgress();
        }

	function loading() {
		var element = document.getElementById("rotate");
		var r = 0;
		var i = 10;
		function rotate(){
		    r += i;
		    if( Math.abs(r) >= 360 ) {
		        r = 0;
		    }
		        
		    element.style.MozTransform = "rotate(" + r + "deg)";
		    element.style.webkitTransform ="rotate(" + r + "deg)";
		    element.style.msTransform = "rotate(" + r + "deg)";
		    element.style.OTransform = "rotate(" + r + "deg)";
		    element.style.transform = "rotate(" + r + "deg)";
		}

		timerloading = setInterval(rotate,20);
	}
	
	function updateProgress() {

		function update() {
			//调用后台接口获取进度
			var element = document.getElementById("progress");
			if(element.innerHTML != "安装中")
				element.innerHTML = uploadProgress+" %";

			if(uploadProgress ==100 && element.innerHTML != "安装中") {
				element.innerHTML = "安装中";
				var loadmsg = document.getElementById("loadmsg");
                loadmsg.style.visibility = "hidden";
				getapkinfo(uploadFileName);
			}
			//判断如果安装成功  errorCode

			if(uploadProgress >=100 && serverResultApkInfo != null) {
				clearInterval(uploadTimer);
				clearInterval(timerloading);
				timerloading = null;
				uploadTimer = null;

				if(serverResultApkInfo.error_code == 0) {  //安装成功
					uploadsuccess();
				} else {			//安装失败
					alert("安装失败："+serverResult.error_msg)
					uploaderror();
				}
			}
		}

		uploadTimer = setInterval(update,1000);
	}

	//上传成功显示成功界面
	function uploadsuccess() {
		var loadingarea = document.getElementById("loadingarea");
		var updatesuccess = document.getElementById("updatesuccess");
		var element = document.getElementById("progress");
		loadingarea.style.display = 'none';
		updatesuccess.style.display = 'block';
		element.innerHTML = "发送中";

		//调用后台接口获取应用信息
		var appImg = document.getElementById("appimg");
		var appName = document.getElementById("appName");
		var appversion = document.getElementById("appversion");
		var appinfo = document.getElementById("appinfo");
		appImg.src = serverResultApkInfo.icon;
		appName.innerHTML = serverResultApkInfo.name;
		appversion.innerHTML = "软件版本："+serverResultApkInfo.version;
		appinfo.innerHTML = "软件大小："+serverResultApkInfo.size/1000 +"MB";
	}

	//如果失败返回初始界面
	function uploaderror() {
			if(uploadTimer != null) {
				clearInterval(uploadTimer);
				uploadTimer = null;
			}

			if(timerloading != null) {
				clearInterval(timerloading);
                timerloading = null;
			}

			var selected = document.getElementById("selected");
        	var uploadarea = document.getElementById("uploadarea");
        	var loadingarea = document.getElementById("loadingarea");
        	var updatesuccess = document.getElementById("updatesuccess");
        	var loadmsg = document.getElementById("loadmsg");
        	var preview = document.getElementById("dz-preview");
        	if(preview != null)
            	preview.style.display = "none";
            loadmsg.style.visibility = "hidden";
        	selected.style.display = "block";
        	uploadarea.style.display = "block";
        	updatesuccess.style.display = "none";
        	loadingarea.style.display = "none";
	}

	function uploadAgain() {
		uploaderror();
	}

	function btnOnMouseOver() {
//		var loadmsg = document.getElementById("inputbtn");
//        loadmsg.src = "../pc_images/uploadbtnselect.png";
		$("#inputbtn").css("background",'url(' + "../pc_images/uploadbtnselect.png" + ')'+'no-repeat center center');
		$("#inputbtn2").css("background",'url(' + "../pc_images/uploadbtnselect.png" + ')'+'no-repeat center center');
	}

	function btnOnMouseOut() {
    		$("#inputbtn").css("background",'url(' + "../pc_images/uploadbtn.png" + ')'+'no-repeat center center');
    		$("#inputbtn2").css("background",'url(' + "../pc_images/uploadbtn.png" + ')'+'no-repeat center center');
    }

	function btnOnMouseDown() {
//		var loadmsg = document.getElementById("inputbtn");
//        loadmsg.src = "../pc_images/uploadbtndown.png";
		$("#inputbtn").css("background",'url(' + "../pc_images/uploadbtndown.png" + ')' +'no-repeat center center');
		$("#inputbtn2").css("background",'url(' + "../pc_images/uploadbtndown.png" + ')' +'no-repeat center center');
    }

    function btnOnMouseUp() {
//		var loadmsg = document.getElementById("inputbtn");
//        loadmsg.src = "../pc_images/uploadbtn.png";
		$("#inputbtn").css("background",'url(' + "../pc_images/uploadbtn.png" + ')'+'no-repeat center center');
		$("#inputbtn2").css("background",'url(' + "../pc_images/uploadbtn.png" + ')'+'no-repeat center center');
    }
